/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_rticonnextdds/listener_impl/DomainParticipantListenerImpl.hpp"
#include "dal_rticonnextdds/entity_impl/DalTopicImpl.hpp"
#include "dal_rticonnextdds/entity_impl/DalDataWriterImpl.hpp"
#include "dal_rticonnextdds/entity_impl/DalDataReaderImpl.hpp"
#include "dal_rticonnextdds/entity_impl/DalSubscriberImpl.hpp"
#include "dal_rticonnextdds/utils/log.hpp"
#include "dal_rticonnextdds/utils/EntityMappingManager.hpp"

void on_publication_matched_dummy2(void* listener_data,
                                   DDS_DataWriter* writer,
                                   const DDS_PublicationMatchedStatus* status)
{
}

void on_subscription_matched_dummy2(void* listener_data,
                                    DDS_DataReader* reader,
                                    const DDS_SubscriptionMatchedStatus* status)
{
}

void on_inconsistent_topic_dummy2(void* listener_data,
                                  DDS_Topic* topic,
                                  const DDS_InconsistentTopicStatus* status)
{
}

void on_liveliness_lost_dummy2(void* listener_data,
                               DDS_DataWriter* writer,
                               const DDS_LivelinessLostStatus* status)
{
}

void on_offered_deadline_missed_dummy2(void* listener_data,
                                       DDS_DataWriter* writer,
                                       const DDS_OfferedDeadlineMissedStatus* status)
{
}

void on_offered_incompatible_qos_dummy2(void* listener_data,
                                        DDS_DataWriter* writer,
                                        const DDS_OfferedIncompatibleQosStatus* status)
{
}

void on_data_on_readers_dummy2(void* listener_data, DDS_Subscriber* sub) {}

void on_sample_lost_dummy2(void* listener_data,
                           DDS_DataReader* reader,
                           const DDS_SampleLostStatus* status)
{
}

void on_data_available_dummy2(void* listener_data, DDS_DataReader* reader) {}

void on_sample_rejected_dummy2(void* listener_data,
                               DDS_DataReader* reader,
                               const DDS_SampleRejectedStatus* status)
{
}

void on_liveliness_changed_dummy2(void* listener_data,
                                  DDS_DataReader* reader,
                                  const DDS_LivelinessChangedStatus* status)
{
}

void on_requested_deadline_missed_dummy2(void* listener_data,
                                         DDS_DataReader* reader,
                                         const DDS_RequestedDeadlineMissedStatus* status)
{
}

void on_requested_incompatible_qos_dummy2(void* listener_data,
                                          DDS_DataReader* reader,
                                          const DDS_RequestedIncompatibleQosStatus* status)
{
}

DomainParticipantListenerImpl::DomainParticipantListenerImpl(
    dal_domain_participant_listener_callbacks_t callbacks)
{
    this->callbacks_ = callbacks;
    participant_monitor_callback_ = nullptr;
    this->rti_partcipant_listener_ = new DDS_DomainParticipantListener();
    *(this->rti_partcipant_listener_) = DDS_DomainParticipantListener_INITIALIZER;

    if (callbacks.on_inconsistent_topic != nullptr) {
        this->rti_partcipant_listener_->as_topiclistener.on_inconsistent_topic =
            DomainParticipantListenerImpl::on_inconsistent_topic;
    } else {
        this->rti_partcipant_listener_->as_topiclistener.on_inconsistent_topic =
            DomainParticipantListenerImpl::on_inconsistent_topic_dummy;
    }

    if (callbacks.on_publication_matched != nullptr) {
        this->rti_partcipant_listener_->as_publisherlistener.as_datawriterlistener
            .on_publication_matched = DomainParticipantListenerImpl::on_publication_matched;
    } else {
        this->rti_partcipant_listener_->as_publisherlistener.as_datawriterlistener
            .on_publication_matched = DomainParticipantListenerImpl::on_publication_matched_dummy;
    }

    if (callbacks.on_liveliness_lost != nullptr) {
        this->rti_partcipant_listener_->as_publisherlistener.as_datawriterlistener
            .on_liveliness_lost = DomainParticipantListenerImpl::on_liveliness_lost;
    } else {
        this->rti_partcipant_listener_->as_publisherlistener.as_datawriterlistener
            .on_liveliness_lost = DomainParticipantListenerImpl::on_liveliness_lost_dummy;
    }

    if (callbacks.on_offered_deadline_missed != nullptr) {
        this->rti_partcipant_listener_->as_publisherlistener.as_datawriterlistener
            .on_offered_deadline_missed = DomainParticipantListenerImpl::on_offered_deadline_missed;
    } else {
        this->rti_partcipant_listener_->as_publisherlistener.as_datawriterlistener
            .on_offered_deadline_missed =
            DomainParticipantListenerImpl::on_offered_deadline_missed_dummy;
    }

    if (callbacks.on_offered_incompatible_qos != nullptr) {
        this->rti_partcipant_listener_->as_publisherlistener.as_datawriterlistener
            .on_offered_incompatible_qos =
            DomainParticipantListenerImpl::on_offered_incompatible_qos;
    } else {
        this->rti_partcipant_listener_->as_publisherlistener.as_datawriterlistener
            .on_offered_incompatible_qos =
            DomainParticipantListenerImpl::on_offered_incompatible_qos_dummy;
    }

    if (callbacks.on_subscription_matched != nullptr) {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_subscription_matched = DomainParticipantListenerImpl::on_subscription_matched;
    } else {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_subscription_matched = DomainParticipantListenerImpl::on_subscription_matched_dummy;
    }

    if (callbacks.on_data_on_readers != nullptr) {
        this->rti_partcipant_listener_->as_subscriberlistener.on_data_on_readers =
            DomainParticipantListenerImpl::on_data_on_readers;
    } else {
        this->rti_partcipant_listener_->as_subscriberlistener.on_data_on_readers =
            DomainParticipantListenerImpl::on_data_on_readers_dummy;
    }

    if (callbacks.on_sample_lost != nullptr) {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener.on_sample_lost =
            DomainParticipantListenerImpl::on_sample_lost;
    } else {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener.on_sample_lost =
            DomainParticipantListenerImpl::on_sample_lost_dummy;
    }

    if (callbacks.on_data_available != nullptr) {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_data_available = DomainParticipantListenerImpl::on_data_available;
    } else {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_data_available = DomainParticipantListenerImpl::on_data_available_dummy;
    }

    if (callbacks.on_sample_rejected != nullptr) {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_sample_rejected = DomainParticipantListenerImpl::on_sample_rejected;
    } else {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_sample_rejected = DomainParticipantListenerImpl::on_sample_rejected_dummy;
    }

    if (callbacks.on_liveliness_changed != nullptr) {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_liveliness_changed = DomainParticipantListenerImpl::on_liveliness_changed;
    } else {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_liveliness_changed = DomainParticipantListenerImpl::on_liveliness_changed_dummy;
    }

    if (callbacks.on_requested_deadline_missed != nullptr) {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_requested_deadline_missed =
            DomainParticipantListenerImpl::on_requested_deadline_missed;
    } else {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_requested_deadline_missed =
            DomainParticipantListenerImpl::on_requested_deadline_missed_dummy;
    }

    if (callbacks.on_requested_incompatible_qos != nullptr) {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_requested_incompatible_qos =
            DomainParticipantListenerImpl::on_requested_incompatible_qos;
    } else {
        this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener
            .on_requested_incompatible_qos =
            DomainParticipantListenerImpl::on_requested_incompatible_qos_dummy;
    }

    this->rti_partcipant_listener_->as_topiclistener.as_listener.listener_data =
        reinterpret_cast<void*>(&this->callbacks_);

    this->rti_partcipant_listener_->as_publisherlistener.as_datawriterlistener.as_listener
        .listener_data = reinterpret_cast<void*>(&this->callbacks_);

    this->rti_partcipant_listener_->as_subscriberlistener.as_datareaderlistener.as_listener
        .listener_data = reinterpret_cast<void*>(&this->callbacks_);
}

DomainParticipantListenerImpl::~DomainParticipantListenerImpl()
{
    if (this->rti_partcipant_listener_) {
        delete this->rti_partcipant_listener_;
        this->rti_partcipant_listener_ = nullptr;
    }
}

DDS_DomainParticipantListener* DomainParticipantListenerImpl::getListener()
{
    return this->rti_partcipant_listener_;
}

void DomainParticipantListenerImpl::on_inconsistent_topic(void* listener_data,
                                                          DDS_Topic* topic,
                                                          const DDS_InconsistentTopicStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_inconsistent_topic == nullptr)
        return;

    dal_topic_t* dal_topic = EntityMappingGetTopic(topic);
    if (!dal_topic) {
        return;
    }

    dal_inconsistent_topic_status_t dal_status;
    StatusConverter::GetInstance()->ConvertInconsistentTopicStatusFromRtiddsToDal(status,
                                                                                  &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_inconsistent_topic)(dal_topic, &dal_status, data);
}

void DomainParticipantListenerImpl::on_offered_deadline_missed(
    void* listener_data, DDS_DataWriter* writer, const DDS_OfferedDeadlineMissedStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_offered_deadline_missed == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_offered_deadline_missed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertOfferedDeadlineMissedStatusFromRtiddsToDal(status,
                                                                                      &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_offered_deadline_missed)(dal_writer, &dal_status, data);
}

void DomainParticipantListenerImpl::on_liveliness_lost(void* listener_data,
                                                       DDS_DataWriter* writer,
                                                       const DDS_LivelinessLostStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_liveliness_lost == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_liveliness_lost_status_t dal_status;
    StatusConverter::GetInstance()->ConvertLivelinessLostStatusFromRtiddsToDal(status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_liveliness_lost)(dal_writer, &dal_status, data);
}

void DomainParticipantListenerImpl::on_offered_incompatible_qos(
    void* listener_data, DDS_DataWriter* writer, const DDS_OfferedIncompatibleQosStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_offered_incompatible_qos == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_offered_incompatible_qos_status_t dal_status;
    StatusConverter::GetInstance()->ConvertOfferedIncompatibleQosStatusFromRtiddsToDal(status,
                                                                                       &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_offered_incompatible_qos)(dal_writer, &dal_status, data);
}

void DomainParticipantListenerImpl::on_publication_matched(
    void* listener_data, DDS_DataWriter* writer, const DDS_PublicationMatchedStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_publication_matched == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_publication_matched_status_t dal_status;
    StatusConverter::GetInstance()->ConvertPublicationMatchedStatusFromRtiddsToDal(status,
                                                                                   &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_publication_matched)(dal_writer, &dal_status, data);
}

void DomainParticipantListenerImpl::on_data_on_readers(void* listener_data, DDS_Subscriber* sub)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_data_on_readers == nullptr)
        return;

    dal_subscriber_t* dal_sub = EntityMappingGetSubscriber(sub);
    if (!dal_sub) {
        return;
    }

    void* data = callbacks->data_;
    (callbacks->on_data_on_readers)(dal_sub, data);
}

void DomainParticipantListenerImpl::on_sample_lost(void* listener_data,
                                                   DDS_DataReader* reader,
                                                   const DDS_SampleLostStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_sample_lost == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_sample_lost_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSampleLostStatusFromRtiddsToDal(status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_sample_lost)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_data_available(void* listener_data, DDS_DataReader* reader)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_data_available == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    void* data = callbacks->data_;
    (callbacks->on_data_available)(dal_reader, data);
}

void DomainParticipantListenerImpl::on_sample_rejected(void* listener_data,
                                                       DDS_DataReader* reader,
                                                       const DDS_SampleRejectedStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_sample_rejected == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_sample_rejected_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSampleRejectedStatusFromRtiddsToDal(status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_sample_rejected)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_liveliness_changed(void* listener_data,
                                                          DDS_DataReader* reader,
                                                          const DDS_LivelinessChangedStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_liveliness_changed == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_liveliness_changed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertLivelinessChangedStatusFromRtiddsToDal(status,
                                                                                  &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_liveliness_changed)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_requested_deadline_missed(
    void* listener_data, DDS_DataReader* reader, const DDS_RequestedDeadlineMissedStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_requested_deadline_missed == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_requested_deadline_missed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertRequestedDeadlineMissedStatusFromRtiddsToDal(
        status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_requested_deadline_missed)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_requested_incompatible_qos(
    void* listener_data, DDS_DataReader* reader, const DDS_RequestedIncompatibleQosStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_requested_incompatible_qos == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_requested_incompatible_qos_status_t dal_status;
    StatusConverter::GetInstance()->ConvertRequestedIncompatibleQosStatusFromRtiddsToDal(
        status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_requested_incompatible_qos)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_subscription_matched(
    void* listener_data, DDS_DataReader* reader, const DDS_SubscriptionMatchedStatus* status)
{
    dal_domain_participant_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_domain_participant_listener_callbacks_t*>(listener_data);
    if (callbacks->on_subscription_matched == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_subscription_matched_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSubscriptionMatchedStatusFromRtiddsToDal(status,
                                                                                    &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_subscription_matched)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_publication_matched_dummy(
    void* listener_data, DDS_DataWriter* writer, const DDS_PublicationMatchedStatus* status)
{
}

void DomainParticipantListenerImpl::on_subscription_matched_dummy(
    void* listener_data, DDS_DataReader* reader, const DDS_SubscriptionMatchedStatus* status)
{
}

void DomainParticipantListenerImpl::on_inconsistent_topic_dummy(
    void* listener_data, DDS_Topic* topic, const DDS_InconsistentTopicStatus* status)
{
}

void DomainParticipantListenerImpl::on_liveliness_lost_dummy(void* listener_data,
                                                             DDS_DataWriter* writer,
                                                             const DDS_LivelinessLostStatus* status)
{
}

void DomainParticipantListenerImpl::on_offered_deadline_missed_dummy(
    void* listener_data, DDS_DataWriter* writer, const DDS_OfferedDeadlineMissedStatus* status)
{
}

void DomainParticipantListenerImpl::on_offered_incompatible_qos_dummy(
    void* listener_data, DDS_DataWriter* writer, const DDS_OfferedIncompatibleQosStatus* status)
{
}

void DomainParticipantListenerImpl::on_data_on_readers_dummy(void* listener_data,
                                                             DDS_Subscriber* sub)
{
}

void DomainParticipantListenerImpl::on_sample_lost_dummy(void* listener_data,
                                                         DDS_DataReader* reader,
                                                         const DDS_SampleLostStatus* status)
{
}

void DomainParticipantListenerImpl::on_data_available_dummy(void* listener_data,
                                                            DDS_DataReader* reader)
{
}

void DomainParticipantListenerImpl::on_sample_rejected_dummy(void* listener_data,
                                                             DDS_DataReader* reader,
                                                             const DDS_SampleRejectedStatus* status)
{
}

void DomainParticipantListenerImpl::on_liveliness_changed_dummy(
    void* listener_data, DDS_DataReader* reader, const DDS_LivelinessChangedStatus* status)
{
}

void DomainParticipantListenerImpl::on_requested_deadline_missed_dummy(
    void* listener_data, DDS_DataReader* reader, const DDS_RequestedDeadlineMissedStatus* status)
{
}

void DomainParticipantListenerImpl::on_requested_incompatible_qos_dummy(
    void* listener_data, DDS_DataReader* reader, const DDS_RequestedIncompatibleQosStatus* status)
{
}

void DomainParticipantListenerImpl::set_participant_monitor_callback(
    dal_participant_monitor_callback callback)
{
    this->participant_monitor_callback_ = callback;
    this->participant_monitor_callback_is_set_ = true;
}

void DomainParticipantListenerImpl::set_participant_monitor_callback_data(void* data)
{
    this->participant_monitor_callback_data_ = data;
}

dal_participant_monitor_callback DomainParticipantListenerImpl::get_participant_monitor_callback()
{
    return this->participant_monitor_callback_;
}

void* DomainParticipantListenerImpl::get_participant_monitor_callback_data()
{
    return this->participant_monitor_callback_data_;
}

bool DomainParticipantListenerImpl::is_participant_monitor_callback_set()
{
    return this->participant_monitor_callback_is_set_;
}
